package com.sunriz.javafx.views;

import javafx.animation.AnimationTimer;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.util.Random;

/**
 * ClassName: RealTimeDataExampleController
 * Package: com.sunriz.javafx
 * Description:
 *
 * @Author BuTang
 * @Create 2024/12/6 14:44
 * @Version 1.0
 */
public class RealTimeDataExampleController {
    private static final int DATA_SIZE = 100;
    private static final double WIDTH = 800;
    private static final double HEIGHT = 600;

    private XYChart.Series<Number, Number> series;
    private ObservableList<XYChart.Data<Number, Number>> data;
    private AnimationTimer animationTimer;
    private boolean isPaused = false;  // 控制动画暂停状态
    private int x = 0;
    private final Random random = new Random();



    public void start(Stage primaryStage) {
        // 初始化数据和系列
        data = FXCollections.observableArrayList();
        series = new XYChart.Series<>();
        series.setData(data);


        // 创建x轴和y轴
        NumberAxis xAxis = new NumberAxis();
        NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Time");
        yAxis.setLabel("Value");

        // 设置x轴的自动滚动和拖动支持
        xAxis.setAutoRanging(false);  // 禁止x轴自动缩放
        xAxis.setLowerBound(0);  // 设置x轴的初始起始位置
        xAxis.setUpperBound(DATA_SIZE);  // 设置x轴的初始结束位置

        // 创建折线图
        LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
        lineChart.setTitle("Real-time Waveform Chart");
        lineChart.getData().add(series);

        // 创建暂停/继续按钮
        Button pauseButton = new Button("暂停");
        pauseButton.setOnAction(e -> toggleAnimation(pauseButton));  // 点击按钮时切换状态

        // 布局
        StackPane root = new StackPane();
        root.getChildren().addAll(lineChart, pauseButton);
        pauseButton.setTranslateY(HEIGHT / 2 - 50);  // 调整按钮位置
        pauseButton.setTranslateX(WIDTH / 2 - 50);

        // 设置场景和舞台
        Scene scene = new Scene(root, WIDTH, HEIGHT);
        primaryStage.setScene(scene);
        primaryStage.setTitle("电机模型图");
        primaryStage.show();

        // 启动动画定时器更新数据
        animationTimer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                if (!isPaused) {
                    // 生成新的随机数
                    double y = random.nextDouble();

                    // 添加新的数据点
                    XYChart.Data<Number, Number> newData = new XYChart.Data<>(x, y);
                    data.add(newData); // 添加到 data 集合

                    // 保持数据点数量为 DATA_SIZE
                    if (data.size() > DATA_SIZE) {
                        data.remove(0); // 删除最旧的数据点
                    }

                    // 更新x轴的范围，滚动视图
                    if (x > xAxis.getUpperBound()) {
                        xAxis.setUpperBound(x + 1);
                        xAxis.setLowerBound(x - DATA_SIZE + 1);  // 让图表向右滚动
                    }

                    // 更新x值
                    x++;
                }
            }
        };
        animationTimer.start();
    }

    // 切换动画的暂停和继续状态
    private void toggleAnimation(Button pauseButton) {
        isPaused = !isPaused;
        if (isPaused) {
            pauseButton.setText("继续");
        } else {
            pauseButton.setText("暂停");
        }
    }
}
